<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>24 Promise基础</title>
</head>

<body>
    <h2>1.19 Promis</h2>
    <p>Promise是ES6引入的异步编程的新解决方案。语法上Promise是一个构造函数，用来封装异步操作并可以获取其成功或失败的结果。</p>
    <ul>
        <li>Promise构造函数：Promise(excutor){}</li>
        <li>Promise.prototype.then 方法</li>
        <li>Promise.prototype.catch 方法</li>
    </ul>

    <script>
        const p = new Promise(function (resolve, reject) {
            setTimeout(function () {
                let data = '用户数据';
                // resolve(data);

                let err = '数据读取失败';
                reject(err);
            }, 1000);
        });

        // 调用 promise对象的 then 方法
        p.then(function (value) {
            console.log(value);
        }, function (reason) {
            console.error(reason);
        })
    </script>
</body>

</html>